package no.ntnu.fp.gui.calendar;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeSupport;
import java.util.Calendar;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class SelectMonthPanel extends JPanel {
	public final static String MONTH_PROPERTY = "month", YEAR_PROPERTY = "year";
	private PropertyChangeSupport pcs;
	private JLabel monthYear;
	private JButton previousMonth, nextMonth;
	private ImageIcon left, right;
//	private int month, year;
	private final String[] months = {"Januar", "Februar", "Mars", "April", "Mai", 
			"Juni", "Juli", "August", "September", "Oktober", "November", "Desember"};
	
	private final Calendar calendar;
	
	public SelectMonthPanel(Calendar calendar){
		this.calendar = calendar;
		pcs = new PropertyChangeSupport(this);
//		this.month = month;
//		this.year = year;
		monthYear = new JLabel(months[getMonth()] + " " + getYear());
		monthYear.setFont(new Font("Tahoma", Font.BOLD, 15));
		left = new ImageIcon("images/changeLeft4.png");
		right = new ImageIcon("images/changeRight4.png");
		previousMonth = new JButton(left);
//		previousMonth.addActionListener(new PreviousMonthAction());
		nextMonth = new JButton(right);
//		nextMonth.addActionListener(new NextMonthAction());
		this.add(previousMonth);
		this.add(monthYear);
		this.add(nextMonth);
	}
	
	public void updateDate() {
		monthYear.setText(months[getMonth()] + " " + getYear());
	}
	
	public void setSelectMonthListeners(ActionListener next,
										ActionListener previous) {
		previousMonth.addActionListener(previous);
		nextMonth.addActionListener(next);
	}
	
	private int getMonth() {
		return calendar.get(Calendar.MONTH);
	}
	
	private int getYear() {
		return calendar.get(Calendar.YEAR);
	}
	
//	public void setMonthAndYear(int month, int year) {
//		this.month = month; this.year = year;
//		this.monthYear.setText(months[month] + " " + year);
//	}
//	
//	private final class PreviousMonthAction implements ActionListener{
//		@Override
//		public void actionPerformed(ActionEvent arg0) {
//			if(month == 0){
//				year--;
//				month = 11;
//				pcs.firePropertyChange(YEAR_PROPERTY, year + 1, year);
//				pcs.firePropertyChange(MONTH_PROPERTY, 0, month);
//			}else{
//				month--;
//				pcs.firePropertyChange(MONTH_PROPERTY, month + 1, month);
//			}
//			monthYear.setText(months[month] + " " + year);
//		}
//	}
//	private final class NextMonthAction implements ActionListener{
//		@Override
//		public void actionPerformed(ActionEvent e) {
//			if(month == 11){
//				year++;
//				month = 0;
//				pcs.firePropertyChange(MONTH_PROPERTY, 11, month);
//				pcs.firePropertyChange(YEAR_PROPERTY, year - 1, year);
//			}else{
//				month++;
//				pcs.firePropertyChange(MONTH_PROPERTY, month - 1, month);
//			}
//			monthYear.setText(months[month] + " " + year);
//		}
//	}
//	public void addPropertyChangeListener(PropertyChangeListener listener){
//		pcs.addPropertyChangeListener(listener);
//	}
}
